home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Beginning Mac Programming
/
Beginning Mac Programming.bin
/
Open Me for REALbasic 3
/
REALbasic 3.2
/
Example Projects
/
Techniques
/
Example by Cortis Clark
/
Make your code faster!
/
Mandelbrot in C
/
Mandelbrot.cpp
< prev
next >
Wrap
Text File
|
2000-04-07
|
2KB
|
117 lines
// by G. Cortis Clark
#include <Palettes.h>
#include "Mandelbrot.h"
inline complex complexSquare (complex c) {
complex cSq;
cSq.x = c.x * c.x - c.y * c.y;
cSq.y = 2 * c.x * c.y;
return (cSq);
}
int iterate (complex zInit) {
complex z = zInit;
int cnt = 0;
// quit when z * z > 4
while ((z.x * z.x + z.y * z.y <= 4.0) && (cnt < MAX)) {
if (zInit.x * zInit.x + zInit.y * zInit.y <= 4.0) {
z = complexSquare(z);
z.x += zInit.x;
z.y += zInit.y;
cnt++;
} else {
z = complexSquare(z);
z.x += 4;
z.y += 4;
cnt++;
}
}
return (cnt);
}
void mandelbrot (int cols, int rows, float rMin, float rMax,
float iMin, float iMax) {
float rInc = (rMax - rMin) / cols;
float iInc = (iMax - iMin) / rows;
int x, y, oldCount = -1, count = 0, runLength = 0;
complex zInit;
PenMode(patCopy);
zInit.x = rMin;
for (x = 0; x < cols; x++) {
zInit.y = iMin;
MoveTo(x, 0);
for (y = 0; y < rows; y++) {
count = iterate (zInit);
if (count != oldCount) {
Line(0, runLength);
PmForeColor((count < 256) ? ((count + 60) % 256): 0);
runLength = 0;
oldCount = count;
}
runLength++;
zInit.y += iInc;
}
Line(0, runLength);
runLength = 0;
zInit.x += rInc;
}
}
/* slow code
complex complexSquare (complex c) {
complex cSq;
cSq.x = c.x * c.x - c.y * c.y;
cSq.y = 2 * c.x * c.y;
return (cSq);
}
int iterate (complex zInit) {
complex z = zInit;
int cnt = 0;
// quit when z * z > 4
while ((z.x * z.x + z.y * z.y <= 4.0) && (cnt < MAX)) {
if (zInit.x * zInit.x + zInit.y * zInit.y <= 4.0) {
z = complexSquare(z);
z.x += zInit.x;
z.y += zInit.y;
cnt++;
} else {
z = complexSquare(z);
z.x += 4;
z.y += 4;
cnt++;
}
}
return (cnt);
}
void mandelbrot (int cols, int rows, double rMin, double rMax,
double iMin, double iMax) {
double rInc = (rMax - rMin) / cols;
double iInc = (iMax - iMin) / rows;
int x, y, count;
complex zInit;
PenMode(patCopy);
zInit.x = rMin;
for (x = 0; x < cols; x++) {
zInit.y = iMin;
for (y = 0; y < rows; y++) {
count = iterate (zInit);
PmForeColor((count < 256) ? ((count + 60) % 256): 0);
MoveTo(x, y);
Line(1, 0);
zInit.y += iInc;
}
zInit.x += rInc;
}
}*/